﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using LocalServer.Model;
using Microsoft.Office.Interop.Excel;
using Newtonsoft.Json.Linq;
using WpfApp2;
using static System.Net.Mime.MediaTypeNames;

namespace LocalServer.Utils
{
    public class MessageDealUtils
    {
        string leftIP = App.GetConfig().Camera_Left_VideoUrl;
        string rightIP = App.GetConfig().Camera_Right_VideoUrl;
        static SpeechSynthesizer speechSynthesizer = null;
        static string ledIp = "";
        static int Leftcode = MainWindow.Leftcode;
        static int Rightcode = MainWindow.Rightcode;


        public static void messageDeal(JObject jObject)
        {

            JObject jsonObj = JObject.Parse(jObject.ToString());
            String type = (String)jsonObj.GetValue("type");
            String direction = (String)jsonObj.GetValue("direction");

            //播报
            if ("speak".Equals(type))
            {
                if (speechSynthesizer == null)
                {
                    speechSynthesizer = new SpeechSynthesizer();
                }
                String voice = (String)jsonObj.GetValue("voice");
                speechSynthesizer.Speak(voice);
            }
            //显示内容
            else if ("ledMessage".Equals(type))
            {
                if ("left".Equals(direction))
                {
                    ledIp = App.GetConfig().LEDIP1;
                    //MessageBox.Show(direction + ledIp);

                }
                else
                {
                    ledIp = App.GetConfig().LEDIP2;
                    //MessageBox.Show(direction + ledIp);


                }
                String message = (String)jsonObj.GetValue("message");
                String areaId = (String)jsonObj.GetValue("areaId");
                String color = (String)jsonObj.GetValue("color");

                RefreshData refreshData = new RefreshData();
                refreshData.areaId = int.Parse(areaId);//区域ID必须与节目中的静态文本区域ID一致才能刷新
                refreshData.text = message;
                refreshData.fontColor = "#ff0000";
                refreshData.refreshTimes = 0;
                refreshData.base64 = 0;

                LedUtil.refreshTextArea(refreshData, ledIp);
            }
            //播报并且显示
            else if ("ledMessageAndSpeak".Equals(type))
            {
                if ("left".Equals(direction))
                {
                    ledIp = App.GetConfig().LEDIP1;
                    //MessageBox.Show(direction + ledIp);

                }
                else
                {
                    ledIp = App.GetConfig().LEDIP2;
                    //MessageBox.Show(direction + ledIp);


                }


                if (speechSynthesizer == null)
                {
                    speechSynthesizer = new SpeechSynthesizer();
                }

                String voice = (String)jsonObj.GetValue("voice");
                JArray array = (JArray)jsonObj.GetValue("ledMsgs");
                String code = (String)jsonObj.GetValue("code");


                if (!"".Equals(voice))
                {
                    speechSynthesizer.Speak(voice);
                }
                for (int i = 0; i < array.Count; i++)
                {

                    JObject item = (JObject)array[i];
                    //    Console.WriteLine(item);
                    String text = (String)item.GetValue("text");
                    int areaId = (int)item.GetValue("areaId");
                    String color = (String)item.GetValue("color");

                    RefreshData refreshData = new RefreshData();
                    refreshData.areaId = areaId;//区域ID必须与节目中的静态文本区域ID一致才能刷新
                    refreshData.text = text;
                    refreshData.fontColor = "#ff0000";
                    refreshData.refreshTimes = 0;
                    refreshData.base64 = 0;

                    LedUtil.refreshTextArea(refreshData, ledIp);
                }


            }
            else if ("weight".Equals(type))
            {
                if ("left".Equals(direction))
                {
                    ledIp = App.GetConfig().LEDIP1;

                }
                else
                {
                    ledIp = App.GetConfig().LEDIP2;


                }
                String weight = (String)jsonObj.GetValue("weight");
                String steady = (String)jsonObj.GetValue("steady");

                RefreshData refreshData = new RefreshData();
                refreshData.areaId = 11;//区域ID必须与节目中的静态文本区域ID一致才能刷新
                refreshData.text = weight;
                if ("1".Equals(steady))
                {
                    refreshData.fontColor = "#00ff00";

                }
                else
                {
                    refreshData.fontColor = "#ff0000";
                }
                refreshData.refreshTimes = 0;
                refreshData.base64 = 0;

                LedUtil.refreshTextArea(refreshData, ledIp);
            }
            else if ("openDevice".Equals(type))
            {
                if ("1".Equals(App.GetConfig().IDCard))
                {
                    String port = "";
                    if ("left".Equals(direction))
                    {
                        port = App.GetConfig().IDCardPort1;
                    }
                    else
                    {
                        port = App.GetConfig().IDCardPort2;
                    }

                    String rate = App.GetConfig().IDCardPortBaudRate;
                    int handle = CommonInterface.OpenDevice(port, rate, "");
                    if (handle <= 0)
                    {
                        MessageBox.Show("打开设备失败，错误代码：" + handle.ToString());
                        return;
                    }
                }
            }
            else if ("readCardNo".Equals(type))
            {
                //读身份证
                string facId = "";
                if ("1".Equals(App.GetConfig().IDCard))
                {
                    String port = "";
                    if ("left".Equals(direction))
                    {
                        port = App.GetConfig().IDCardPort1;
                    }
                    else
                    {
                        port = App.GetConfig().IDCardPort2;
                    }

                    String rate = App.GetConfig().IDCardPortBaudRate;
                    int handle = CommonInterface.OpenDevice(port, rate, "");
                    if (handle <= 0)
                    {
                        MessageBox.Show("打开设备失败，错误代码：" + handle.ToString());
                        return;
                    }

                    if ("left".Equals(direction))
                    {
                        ledIp = App.GetConfig().LEDIP1;

                    }
                    else
                    {
                        ledIp = App.GetConfig().LEDIP2;
                    }
                    //取身份证号
                    StringBuilder IdCardInfo = new StringBuilder(409600);
                    int ret = CommonInterface.SdtReadCard(0, 1, IdCardInfo, 0);
                    if (ret == 0)
                    {
                        string[] CardInfo = IdCardInfo.ToString().Split(':');

                        String name = CardInfo[1];
                        String idNum = CardInfo[9];
                        facId = idNum;
/*                        //MessageBox.Show(idNum);
                        string cardNo= facId;
                        JObject reqObj = new JObject();
                        reqObj.Add("facCardNo", cardNo);
                        SendMessageUtils.SendNodeRedMsg("/PostMessage/facCardNo", reqObj);*/

                        RefreshData refreshData = new RefreshData();
                        refreshData.areaId = 3;//区域ID必须与节目中的静态文本区域ID一致才能刷新
                        refreshData.text = idNum;
                        refreshData.fontColor = "#ff0000";
                        refreshData.refreshTimes = 0;
                        refreshData.base64 = 0;

                        LedUtil.refreshTextArea(refreshData, ledIp);


                    }



                }
                else
                {
                    //读默认身份证
                    facId = App.GetConfig().IDCardNumber;
                }

                if ("1".Equals(App.GetConfig().ICCard))
                {
                    //读取IC卡
                    short port = short.Parse(App.GetConfig().ICCardPort);
                    IntPtr icdev = Reader.hc_init(port, 115200);
                    try
                    {
                        if (icdev.ToInt32() > 0)
                        {
                            StringBuilder ver = new StringBuilder(32);
                            Reader.srd_ver(icdev, 0, ver);
                            Reader.dv_beep(icdev, 30);

                            byte snrlen = 0;
                            byte[] snr = new byte[7];
                            int st = Reader.rf_scard(icdev, 1, snr, out snrlen);
                            if (st != 0)
                            {
                                //MessageBox.Show(Reader.GetErrMsg(0, st));
                            }
                            else
                            {
                                String cardNo = CardUtil.ByteArrayToHexString(snr, 0, snrlen);
                                facId = cardNo;
                                JObject reqObj = new JObject();
                                reqObj.Add("facCardNo", cardNo);
                                SendMessageUtils.SendNodeRedMsg("/PostMessage/facCardNo", reqObj);
                                RefreshData refreshData = new RefreshData();
                                refreshData.areaId = 3;//区域ID必须与节目中的静态文本区域ID一致才能刷新
                                refreshData.text = cardNo;
                                refreshData.fontColor = "#ff0000";
                                refreshData.refreshTimes = 0;
                                refreshData.base64 = 0;

                                LedUtil.refreshTextArea(refreshData, ledIp);
                            }
                        }
                    }
                    catch (Exception ex) { }

                    Reader.hc_exit(icdev);
                }
                JObject cardNoJson = new JObject();
                //cardNoJson.Add("facCardNo", facId);

                cardNoJson.Add("facCardNo", facId);
                SendMessageUtils.SendNodeRedMsg("PostMessage/facCardNo", cardNoJson);
            }
            else if ("PrintSmallTicket".Equals(type))
            {
                JObject billObj = (JObject)jsonObj.GetValue("billObj");

                PrintUtils.printWeighOrder(billObj);
            }
            else if ("LicensePlate".Equals(type))//车牌识别
            {
                if (direction == "left")
                {
                    VzClientSDK.VzLPRClient_ForceTrigger(Leftcode);
                }
                else if (direction == "right")
                {
                    {
                        VzClientSDK.VzLPRClient_ForceTrigger(Rightcode);
                    }
                }
            }
            else if ("Complete".Equals(type))
            {
                if ("left".Equals(direction))
                {
                    ledIp = App.GetConfig().LEDIP1;
                }
                else
                {
                    ledIp = App.GetConfig().LEDIP2;

                }
                JArray array = (JArray)jsonObj.GetValue("ledMsgs");
                //for (int i = 0; i < array.Count; i++)
                //{
                //    JObject item = (JObject)array[i];
                //    String text = (String)item.GetValue("text");
                //    int areaId = (int)item.GetValue("areaId");
                //    String color = (String)item.GetValue("color");

                //    RefreshData refreshData = new RefreshData();
                //    refreshData.areaId = areaId;//区域ID必须与节目中的静态文本区域ID一致才能刷新
                //    refreshData.text = text;
                //    refreshData.fontColor = "#ff0000";
                //    refreshData.refreshTimes = 0;
                //    refreshData.base64 = 0;

                //    LedUtil.refreshTextArea(refreshData, ledIp);
                //}

            }
            else if ("closeIdDevice".Equals(type))
            {
                int Handle = CommonInterface.GetCurrentDevice();

                int ret1 = CommonInterface.CloseDevice();
                if (ret1 != 0)
                {
                    //MessageBox.Show("关闭设备失败，错误代码：" + ret1.ToString());
                    return;
                }
            }
            }
        }
    }

